package viroyal.com.base.widget

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.util.AttributeSet
import com.tencent.smtt.sdk.WebSettings
import com.tencent.smtt.sdk.WebSettings.LayoutAlgorithm
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient

class X5WebView : WebView {
    private val client = object : WebViewClient() {
        /**
         * 防止加载网页时调起系统浏览器
         */
        override fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean {
            view.loadUrl(url)
            return true
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    constructor(arg0: Context, arg1: AttributeSet) : super(arg0, arg1) {
        this.webViewClient = client
        // this.setWebChromeClient(chromeClient);
        // WebStorage webStorage = WebStorage.getInstance();
        initWebViewSettings()
        this.view.isClickable = true

        //兼容视频
//        try {
//            var data = Bundle()
//            //true表示标准全屏，false表示X5全屏；不设置默认false，
//            data.putBoolean("standardFullScreen", false)
//
//            //false：关闭小窗；true：开启小窗；不设置默认true，
//            data.putBoolean("supportLiteWnd", false)
//            //1：以页面内开始播放，2：以全屏开始播放；不设置默认：1
//            data.putInt("DefaultVideoScreen", 2)
//
//
//            x5WebViewExtension?.invokeMiscMethod("setVideoParams", data)
//        } catch (e: Exception) {
//            e.printStackTrace()
//        }
    }

    private fun initWebViewSettings() {
        val webSetting = this.settings
        webSetting.javaScriptEnabled = true
        webSetting.javaScriptCanOpenWindowsAutomatically = true
        webSetting.allowFileAccess = true
        webSetting.layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS
        webSetting.setSupportZoom(true)
        webSetting.builtInZoomControls = true
        webSetting.useWideViewPort = true
        webSetting.setSupportMultipleWindows(true)
        // webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true)
        // webSetting.setDatabaseEnabled(true);
        webSetting.domStorageEnabled = true
        webSetting.setGeolocationEnabled(true)
        webSetting.setAppCacheMaxSize(java.lang.Long.MAX_VALUE)
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.pluginState = WebSettings.PluginState.ON_DEMAND
        // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSetting.cacheMode = WebSettings.LOAD_NO_CACHE
        webSetting.setUserAgentString("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");

        // this.getSettingsExtension().setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);//extension
        // settings 的设计
    }

    //	@Override
    //	protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    //		boolean ret = super.drawChild(canvas, child, drawingTime);
    //		canvas.save();
    //		Paint paint = new Paint();
    //		paint.setColor(0x7fff0000);
    //		paint.setTextSize(24.f);
    //		paint.setAntiAlias(true);
    //		if (getX5WebViewExtension() != null) {
    //			canvas.drawText(this.getContext().getPackageName() + "-pid:"
    //					+ android.os.Process.myPid(), 10, 50, paint);
    //			canvas.drawText(
    //					"X5  Core:" + QbSdk.getTbsVersion(this.getContext()), 10,
    //					100, paint);
    //		} else {
    //			canvas.drawText(this.getContext().getPackageName() + "-pid:"
    //					+ android.os.Process.myPid(), 10, 50, paint);
    //			canvas.drawText("Sys Core", 10, 100, paint);
    //		}
    //		canvas.drawText(Build.MANUFACTURER, 10, 150, paint);
    //		canvas.drawText(Build.MODEL, 10, 200, paint);
    //		canvas.restore();
    //		return ret;
    //	}

    constructor(arg0: Context) : super(arg0) {
        setBackgroundColor(85621)
    }

}
